// application.cpp
// Customizer_Base
// Protea Customizer/Customizer Pro / Business Logic for managing applications
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "base_setup.h"


//@ SEC 3
//@ CHA Appl Defn Verify
//@ COD ApplDefnVerify
//@ TAB Appl_defn Read
//@ GEN Use to verify appl_defn records

void ApplDefnVerify( XCBuffer* appl_defn, verify_type verify_mode, XCTransactionVars* vars ) {
	TRANS_INFUNC( ApplDefnVerify );

	//@ Force application names to be uppercase
	appl_defn->GetField( FLD_APPL_NAME )->Uppercase();

	//@ SUB ValidateName to check for a valid name
	ValidateName( FLD_APPL_NAME, appl_defn, ERR_APPLICATION, vars );

	//@ If verifying an ADD transaction,
	if (verify_mode == VERIFY_ADD) {
		//@ GRS

		//@ Verify that an application does not already exist
		//@ SUB LookupAppNumber to convert field name into a field number
		//@ ERR C (ALREADY_EXISTS) Application already exists
		if ( LookupNumberFromName( vars, vars->buffer, appl_defn, FLD_APPL_NBR, FLD_APPL_NAME, FMT_APPL_DEFN ) )
			vars->Errors->Add( ERR_ALREADY_EXISTS, SEV_CRITICAL, FLD_APPL_NAME, 1, ERR_APPLICATION );

		//@ GRE
	} else { // Review, Change or delete
		//@ GRS
		
		//@ Verify that the application does exist, if not:
		//@ ERR E (APP_NOT_REG) Application not registered
		if ( ! LookupNumberFromName( vars, vars->buffer, appl_defn, FLD_APPL_NBR, FLD_APPL_NAME, FMT_APPL_DEFN ) ||
			!ApplDefnRetrieve( appl_defn, vars ))
			vars->Errors->Add( ERR_APP_NOT_REG, SEV_CRITICAL, FLD_APPL_NAME );

		//@ GRE
	};

	TRANS_OUTFUNC();
};





//@ SEC 3
//@ CHA Appl Defn Retrieve
//@ COD ApplDefnRetrieve
//@ TAB Appl_defn Read
//@ GEN Use to retrieve a particular appl_defn record

bool ApplDefnRetrieve( XCBuffer* appl_defn, XCTransactionVars* vars ) {
	TRANS_INFUNC( ApplDefnRetrieve );

	bool result;
	XCDBStatement* stmt = NULL;
	XCSQLGen* query = vars->query;

	try {
		XCCompare comptemp;

		query->MakeSELECT( appl_defn );
		query->AddWHERE( appl_defn->GetField( FLD_APPL_NBR ), &comptemp );

		stmt = vars->dbtxn->ExecuteSQL( query );
		result = (!stmt->Eof());
	} catch( ... ) {
		App->CleanupObjects( 1, &stmt );
		throw;
	};
	App->CleanupObjects( 1, &stmt );

	TRANS_OUTFUNCRET( result );
};




//@ SEC 3
//@ CHA Appl Defn Delete
//@ COD ApplDefnDelete
//@ TAB Appl_defn write
//@ GEN Use to delete appl_defn records

void ApplDefnDelete( XCBuffer* appl_defn, XCTransactionVars* vars ) {
	TRANS_INFUNC( ApplDefnDelete );

	XCSQLGen* query = vars->query;

	//@ Delete the application record
	query->MakeFull( SQL_STMT_DELETE, appl_defn );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};
